package com.yun.zf.system.utils;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.java.Log;
import org.springframework.stereotype.Component;

/**
 * 高德地图API
 */
@Component
@Log
public class AmapUtil {
    //在高德申请的应用Key
    private static final String GOULD_KEY= "0b7219a770432a2fce94c9ca423c9eda";

    //申请的账户Key

    /**
     * 0.根据地址名称得到两个地址间的距离
     * @param start 起始位置
     * @param start 结束位置
     * @return 两个地址间的距离
     */
    public long getDistanceByAddress(String start, String end) {
        String startLonLat = getLocation(start,null);
        String endLonLat = getLocation(end,null);
        long dis = getDistance(startLonLat, endLonLat);
        return dis;
    }

    public String getLocation(String address ,String city) {
        // 返回输入地址address的经纬度信息, 格式是 经度,纬度
        String queryUrl = "http://restapi.amap.com/v3/geocode/geo?key="+GOULD_KEY+"&address="+address+"&city="+city ;
        String queryResult = HttpUtil.get(queryUrl); // 高德接品返回的是JSON格式的字符串
        JSONObject job = JSONObject.parseObject(queryResult);
        if (job.getString("status").equals("1")&&!job.getString("count").equals("0")){
            return   JSONArray.parseArray(job.getString("geocodes")).getJSONObject(0).getString("location");
        }
        return null;
    }

    /**
     * 1.地址转换为经纬度
     * @param address 地址
     * @return 经纬度
     */
    public JSONObject getLonLat(String address ,String city) {
        // 返回输入地址address的经纬度信息, 格式是 经度,纬度
        String queryUrl = "http://restapi.amap.com/v3/geocode/geo?key="+GOULD_KEY+"&address="+address+"&city="+city ;
        String queryResult = HttpUtil.get(queryUrl); // 高德接品返回的是JSON格式的字符串
        JSONObject job = JSONObject.parseObject(queryResult);
        if (job.getString("status").equals("1")&&!job.getString("count").equals("0")){
          return   JSONArray.parseArray(job.getString("geocodes")).getJSONObject(0);
        }
        return null;
    }

    /**
     * 将经纬度getLng， getLat 通过getAMapByLngAndLat方法转换地址
     * @return 地址名称
     * @throws Exception
     */
    public static String getAMapByLngAndLat(String location,String poitype,String radius ,String extensions) throws Exception {
        String url;
        try {
            url = "http://restapi.amap.com/v3/geocode/regeo?output=JSON&location=" + location
                    + "&key="+GOULD_KEY+"&radius="+radius+"&extensions="+extensions+"&poitype="+poitype;
            String queryResult = HttpUtil.get(url); // 高德接品返回的是JSON格式的字符串
            if (queryResult == null) {
                return "-1";
            }
            // 将获取结果转为json 数据
            JSONObject obj = JSONObject.parseObject(queryResult);
            if (obj.get("status").toString().equals("1")) {
                // 如果没有返回-1
                JSONObject regeocode = obj.getJSONObject("regeocode");
                if (regeocode.size() > 0) {
                    // 在regeocode中拿到 formatted_address 具体位置
                   // String formatted = regeocode.get("formatted_address").toString();
                    return regeocode.toString();

                } else {
                    System.out.println("未找到相匹配的地址！");
                    return "-1";

                }
            } else {
                System.out.println("请求错误！");
                return "-1";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "-1";
    }

    /**
     * 2.根据两个定位点的经纬度算出两点间的距离
     * @param startLonLat 起始经纬度
     * @param endLonLat 结束经纬度（目标经纬度）
     * @return 两个定位点之间的距离
     */
    private long getDistance(String startLonLat, String endLonLat) {
        // 返回起始地startAddr与目的地endAddr之间的距离，单位：米
        Long result = new Long(0);
        String queryUrl = "http://restapi.amap.com/v3/distance?key="+GOULD_KEY+"&origins=" + startLonLat + "&destination="
                + endLonLat;
        String queryResult = HttpUtil.get(queryUrl);
        JSONObject job = JSONObject.parseObject(queryResult);
        JSONArray ja = job.getJSONArray("results");
        JSONObject jobO = JSONObject.parseObject(ja.getString(0));
        result = Long.parseLong(jobO.get("distance").toString());
//		System.out.println("距离2：" + result);
        return result;
    }

    /**
     *
     * @param keywords 查询关键词
     * @param type POI分类 支持传入多个分类，多个类型剑用“|”分隔可选值：POI分类名称、分类代码
     * @param location 坐标
     * @param city 搜索城市 citycode、adcode，不支持县级市。
     * @param citylimit 仅返回指定城市数据  可选值：true/false
     * @param datatype 返回的数据类型 可选值：all-返回所有数据类型、poi-返回POI数据类型、bus-返回公交站点数据类型、busline-返回公交线路数据类型
     * @return
     */
    public static  JSONObject inputtips(String keywords,String type,String location,String city,Boolean citylimit,String datatype ){
        String queryUrl = "https://restapi.amap.com/v3/assistant/inputtips?key="+GOULD_KEY+"&keywords=" + keywords + "&type="
                + type+ "&location=" + location+ "&city=" + city+ "&citylimit=" + citylimit+ "&datatype=" + datatype;
        String queryResult = HttpUtil.get(queryUrl);
        JSONObject job = JSONObject.parseObject(queryResult);
       String status = job.get("status").toString();
        if ("1".equals(status)){
            return  job;
        }
    return  null;
    }

    /**
     * 获取周边信息
     */
    public static JSONObject  around(String location,String keywords,String types,String city,String radius,int offset , int page ,String extensions){
        String queryUrl = "https://restapi.amap.com/v3/place/around?key="+GOULD_KEY+"&location=" + location + "&keywords="
                + keywords+"&types="+types+"&city="+city+"&radius="+radius+"&offset="+offset+"&page="+page;
        String queryResult = HttpUtil.get(queryUrl);
        JSONObject job = JSONObject.parseObject(queryResult);
        return  job;
    }



    public static void main(String[] args) {
        //114.378973,30.602561
        try {

         /*   String regeocode =    getAMapByLngAndLat("121.250793,31.124599");
            JSONObject job = JSONObject.parseObject(regeocode);
            JSONObject addressComponent = job.getJSONObject("addressComponent");
*/
         /*   JSONObject job =   around("121.415527,31.176919","","150500","","3000",20,1,"");
            JSONArray ja = job.getJSONArray("pois");*/


        }catch (Exception e){

        }

    }

}
